package com.cpr.core.rabbitmq.springctl.xml;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * @author Chen Panrui
 * created at 2022/3/15 22:40
 */
public class ProducerApp {
    public static void main(String[] args) throws UnsupportedEncodingException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-rabbit.xml");
        RabbitTemplate template = context.getBean(RabbitTemplate.class);

        MessagePropertiesBuilder builder = MessagePropertiesBuilder.newInstance();
        builder.setContentEncoding("gbk");
        builder.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);

        for (int i = 0; i < 1000; i++) {
            template.send("ex.spring.direct","routing.directEx.q1", MessageBuilder
                    .withBody((i + "你好，陈潘锐").getBytes("gbk"))
                    .andProperties(builder.build()).build());
        }

    }
}
